| Conditions | 1 |
| Paths | 1 |
| Total Lines | 146 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | module.exports = function(GedcomX){ |
||
| 2 | |||
| 3 | var utils = require('../utils'), |
||
| 4 | Base = require('../Base'); |
||
| 5 | |||
| 6 | /** |
||
| 7 | * A list of Links |
||
| 8 | * |
||
| 9 | * {@link https://github.com/FamilySearch/gedcomx-rs/blob/master/specifications/rs-specification.md#json-type-member|GEDCOM X RS Spec} |
||
| 10 | * |
||
| 11 | * @class Links |
||
| 12 | * @extends Base |
||
| 13 | * @param {Object} [json] |
||
| 14 | */ |
||
| 15 | var Links = function(json){ |
||
| 16 | |||
| 17 | // Protect against forgetting the new keyword when calling the constructor |
||
| 18 | if(!(this instanceof Links)){ |
||
| 19 | return new Links(json); |
||
| 20 | } |
||
| 21 | |||
| 22 | // If the given object is already an instance then just return it. DON'T copy it. |
||
| 23 | if(Links.isInstance(json)){ |
||
| 24 | return json; |
||
| 25 | } |
||
| 26 | |||
| 27 | this.init(json); |
||
|
|
|||
| 28 | }; |
||
| 29 | |||
| 30 | // There's no value in us extending Base at the moment |
||
| 31 | Links.prototype = Object.create(Base.prototype); |
||
| 32 | |||
| 33 | Links._gedxClass = Links.prototype._gedxClass = 'GedcomX.Links'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Check whether the given object is an instance of this class. |
||
| 37 | * |
||
| 38 | * @param {Object} obj |
||
| 39 | * @returns {Boolean} |
||
| 40 | */ |
||
| 41 | Links.isInstance = function(obj){ |
||
| 42 | return utils.isInstance(obj, this._gedxClass); |
||
| 43 | }; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Initialize from JSON |
||
| 47 | * |
||
| 48 | * @param {Object} |
||
| 49 | * @return {Link} this |
||
| 50 | */ |
||
| 51 | Links.prototype.init = function(json){ |
||
| 52 | |||
| 53 | Base.prototype.init.call(this, json); |
||
| 54 | |||
| 55 | this.links = []; |
||
| 56 | |||
| 57 | if(json){ |
||
| 58 | this.setLinks(json); |
||
| 59 | } |
||
| 60 | return this; |
||
| 61 | }; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Get the list of links |
||
| 65 | * |
||
| 66 | * @return {Link[]} links |
||
| 67 | */ |
||
| 68 | Links.prototype.getLinks = function(){ |
||
| 69 | return this.links; |
||
| 70 | }; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Get a link matching a rel |
||
| 74 | * |
||
| 75 | * @param {String} rel |
||
| 76 | * @return {Link} link |
||
| 77 | */ |
||
| 78 | Links.prototype.getLink = function(rel){ |
||
| 79 | return this.links.find(function(link){ |
||
| 80 | return link.getRel() === rel; |
||
| 81 | }); |
||
| 82 | }; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Add a link |
||
| 86 | * |
||
| 87 | * @param {Link} link |
||
| 88 | * @return {Links} this |
||
| 89 | */ |
||
| 90 | Links.prototype.addLink = function(link){ |
||
| 91 | // TODO: check for duplicates |
||
| 92 | this.links.push(GedcomX.Link(link)); |
||
| 93 | return this; |
||
| 94 | }; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Set the links. May either provide the JSON object structure or an array |
||
| 98 | * of Link instances |
||
| 99 | * |
||
| 100 | * @param {Object|Link[]} links |
||
| 101 | * @return {Links} this |
||
| 102 | */ |
||
| 103 | Links.prototype.setLinks = function(links){ |
||
| 104 | this.links = []; |
||
| 105 | if(links){ |
||
| 106 | |||
| 107 | // List of link |
||
| 108 | if(Array.isArray(links)){ |
||
| 109 | for(var i = 0; i < links.length; i++){ |
||
| 110 | this.addLink(links[i]); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | // JSON object |
||
| 115 | else { |
||
| 116 | for(var rel in links){ |
||
| 117 | this.addLink(new GedcomX.Link(links[rel]).setRel(rel)); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | } |
||
| 121 | return this; |
||
| 122 | }; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Export the object as JSON |
||
| 126 | * |
||
| 127 | * @return {Object} JSON object |
||
| 128 | */ |
||
| 129 | Links.prototype.toJSON = function(){ |
||
| 130 | var links = this.getLinks(), |
||
| 131 | json = {}, |
||
| 132 | linkJson, rel; |
||
| 133 | for(var i = 0; i < links.length; i++){ |
||
| 134 | linkJson = utils.toJSON(links[i]); |
||
| 135 | rel = linkJson.rel; |
||
| 136 | if(rel){ |
||
| 137 | delete linkJson.rel; |
||
| 138 | json[rel] = linkJson; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | return json; |
||
| 142 | }; |
||
| 143 | |||
| 144 | GedcomX.Links = Links; |
||
| 145 | |||
| 146 | }; |